package com.linciping.useges.plugin.util;

import java.util.*;

public class CollectionUtil {

    public static <T> boolean isListEmpty(List<T> list) {
        return list == null || list.isEmpty();
    }

    public static <T> boolean isSetEmpty(Set<T> list) {
        return list == null || list.isEmpty();
    }

    public static <K, V> boolean isMapEmpty(Map<K, V> list) {
        return list == null || list.isEmpty();
    }

    public static List<String> getRootPackageList(List<String> packageList) {
        String rootPackage = Collections.min(packageList, (o1, o2) -> {
            int packageLevel1 = o1.split("\\.").length;
            int packageLevel2 = o2.split("\\.").length;
            if (packageLevel1 > packageLevel2) {
                return 1;
            } else if (packageLevel1 == packageLevel2) {
                return 0;
            }
            return -1;
        });
        int rootPackageLevel = rootPackage.split("\\.").length;
        List<String> rootPackageList = new ArrayList<>();
        for (String packageName : packageList) {
            int packageLevel = packageName.split("\\.").length;
            if (rootPackageLevel == packageLevel) {
                rootPackageList.add(packageName);
            }
        }
        return rootPackageList;
    }

//    public static void mergeNode(List<SourceNode> sourceNodeList) {
//        for (SourceNode sourceNode : sourceNodeList) {
//            if (isSingleNode(sourceNode.getChildrenList())) {
//                SourceNode child = sourceNode.getChildrenList().get(0);
//                String firstName = sourceNode.getName();
//                String newName = firstName + "." + child.getName();
//                sourceNode.updateName(newName);
//                sourceNode.getChildrenList().clear();
//                sourceNode.getChildrenList().addAll(child.getChildrenList());
//            }
//            mergeNode(sourceNode.getChildrenList());
//        }
//    }
//
//    private static boolean isSingleNode(List<SourceNode> sourceNodeList) {
//        if (sourceNodeList.size() != 1) return false;
//        SourceNode singleNode = sourceNodeList.get(0);
//        SourceNode parent = singleNode.getParent();
//        return parent != null && parent.getType() == SourceNode.TYPE_PACKAGE && singleNode.getType() == SourceNode.TYPE_PACKAGE;
//    }
}
